package net.zjitc.bookshop15.pojo;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author 董熠涛
 * @date 2025/4/28 14:16
 */
@Slf4j
@Data
//@JsonIgnoreProperties(ignoreUnknown = true)
public class loginUser implements UserDetails {

    private User userone;

    private List<String> perms;

    /*
    * 特别注意，两个注解选一个
    * 原因：在序列化时，Spring Security会将权限列表转换为GrantedAuthority对象列表，
    * 而GrantedAuthority是一个接口，无法直接序列化，需要使用@JsonIgnore或@JSONField(serialize = false)注解来忽略该字段。
    * 以确保在序列化和反序列化时都能正确处理权限列表。
    * 如果是JSONField都要增加deserialize和serialize = false 否则无法实现忽略
    * */

    //deserialize = false  反序列化时忽略该字段  serialize = false 序列化时忽略该字段
//    fastjson 库中的核心类  搭配使用Json工具类使用
    @JSONField(serialize = false, deserialize = false)
//    @JsonIgnore         //序列化和反序列化时都忽略该字段   jackson 库中的核心类  搭配使用objectMapper工具类使用
    private List<GrantedAuthority> authorities;

    public loginUser() {
    }

    public loginUser(User userone, List<String> perms) {
        this.userone = userone;
        this.perms = perms;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        if (authorities != null){
            return authorities;
        }
        authorities = new ArrayList<>();
        for (String perm : perms){
            authorities.add(new SimpleGrantedAuthority(perm));
        }
        return authorities;//        if (authorities != null) {
//            return authorities;
//        }
//        authorities = new ArrayList<>();
//        log.info("权限列表："+perms);
//        /*
//        * 类型转换    String -> SimpleGrantedAuthority
//        * */
//        for(String perm : perms){
//            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority(perm);
//            authorities.add(simpleGrantedAuthority);
//        }
//        log.info("权限列表："+authorities);
//        return authorities;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return userone.getPassword();
    }

    @JsonIgnore
    @Override
    public String getUsername() {
        return userone.getUsername();
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return UserDetails.super.isAccountNonExpired();
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return UserDetails.super.isAccountNonLocked();
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return UserDetails.super.isCredentialsNonExpired();
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return UserDetails.super.isEnabled();
    }
}
